﻿package com.s2paganini.util 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.Font;

	/**
	 * @author siLvio paganini | s2paganini.com
	 * @category: Register all embeded assets
	 * 
	 * <p>
	 * @usage
	 * <br>
	 * <b>Sounds</b><br>
	 * AssetsFactory.registerClass("Reference", LoadEmbedAsset.returnAssetByName(this.loaderInfo, "Sound Library Name"));<br>
	 * var test : Sound = AssetsFactory.getInstance("Reference");<br>
	 * test.play();
	 * <br><br>
	 * <b>Fonts</b><br>
	 * AssetsFactory.registerFont("Reference", this);<br>
	 * var textFormat : TextFormat = new TextFormat(AssetsFactory.getFontName("Reference"));
	 * <br><br>
	 * <b>Bitmap</b><br>
	 * AssetsFactory.registerClass("Reference", LoadEmbedAsset.returnAssetByName(this.loaderInfo, "Bitmap Library Name"));<br>
	 * AssetsFactory.getBitmap("Reference");
	 * <br><br>
	 * <b>General Use</b><br>
	 * AssetsFactory.registerClass("Reference", LoadEmbedAsset.returnAssetByName(this.loaderInfo, "Asset Library Name"));<br>
	 * AssetsFactory.getClass("Reference");<br>
	 * var test : * = AssetsFactory.getInstance("Reference");
	 * <br><br>
	 * <b>Multiple Classes</b><br>
	 * AssetsFactory.registerMultipleClasses(assetsArray, loaderInfoReference);
	 * </p>
	 */
	public class AssetsFactory 
	{
		private static var factory : Object = new Object();

		/**
		 * Register the Embedded Asset
		 * @param className Reference Name to the registered Class
		 * @param classObj Embed Asset to register
		 */
		public static function registerClass(className : String, classObj : *) : void 
		{
			factory[className] = classObj;
		}

		/**
		 * Register range of Embedded Assets 
		 * @param arrayClasses Array with all assets to register
		 * @param target LoaderInfo Reference
		 */
		public static function registerMultipleClasses(arrayClasses : Array, target : *) : void 
		{
			for (var i : int = 0;i < arrayClasses.length; i++) {
				factory[arrayClasses[i]] = LoadEmbedAsset.returnAssetByName(target["loaderInfo"], arrayClasses[i]);
			}
		}

		/**
		 * Returns the Embedded Class
		 * @param className Name of the Embedded Class
		 */
		public static function getClass(className : String) : * 
		{
			return factory[className];	
		}

		/**
		 * Returns a new instance from the Embedded Asset
		 * @param className Name of the Embedded Asset
		 */
		public static function getInstance(className : String) : * 
		{
			return new factory[className];
		}

		/**
		 * Returns Bitmap from BitmapData Embedded
		 * @param className Name of the BitmapData Embedded
		 * @param width Width for the the Bitmap
		 * @param height Height for the Bitmap
		 */		 		 
		public static function getBitmap(className : String, width : int = 0, height : int = 0) : Bitmap 
		{
			return new factory[className](width, height) as Bitmap;
		}

		/**
		 * Returns BitmapData from BitmapData Embedded
		 * @param className Name of the BitmapData Embedded
		 * @param width Width for the the Bitmap
		 * @param height Height for the Bitmap
		 */		 		 
		public static function getBitmapData(className : String, width : int = 0, height : int = 0) : BitmapData
		{
			return new factory[className](width, height) as BitmapData;
		}

		/**
		 * Register Embedded Font
		 * @param className Reference name of the Embedded Font
		 * @param ref Reference from where font is embedded
		 */
		public static function registerFont(className : String, ref : Sprite) : void 
		{
			var temp : * = LoadEmbedAsset.returnAssetByName(ref.loaderInfo, className);
			Font.registerFont(temp);
			factory[className] = temp;
		}

		/**
		 * Register Multiple Fonts from Array
		 * @param arrayFonts Array with all fonts' names
		 * @param ref Reference of the stage
		 */
		public static function registerMultipleFonts(arrayFonts : Array, ref : Sprite, traceFonts : Boolean = false) : void
		{
			for (var i : int = 0;i < arrayFonts.length; i++) {
				var temp : Class = LoadEmbedAsset.returnAssetByName(ref.loaderInfo, arrayFonts[i]);
				Font.registerFont(temp);
				factory[arrayFonts[i]] = temp;	
			}
			
			if(traceFonts) {
				for(var f : int = 0;f < Font.enumerateFonts().length; f++) {
					trace(Font.enumerateFonts()[f].fontName);
				}
			}
		}

		/**
		 * @param className Reference Name of the font
		 * @return Font Name
		 */
		public static function getFontName(className : String) : String
		{
			var tempFont : Font = new factory[className];
			return tempFont.fontName;
		}
	}
}
